#!/usr/bin/perl -w # for syntax highlighting

use gri_secmgr qw(get_cookie_value got_cookie);
use gri_database;
use Data::Dumper;
use MIME::Base64;

sub mm_docrepos_view_document_history_populate {
my $document_sha1=shift;
my $lname=shift;

	my $section=decode_base64($::PARAM{section});
	if(! got_cookie(name => "gri_dbm_intranet")) {
		return "No login information - no data returned!";
	}
	my $v=get_cookie_value(name=>"gri_dbm_intranet");
	if($v =~ /^public\s+/) {
		return "Unable to view documetn history - user logged in as 'public'";
	}
	$v=~ s/\s+.*//;
	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $DB=new gri_database("$db_dir/gri.db");
	# Grab the existing details from the file - which might be edited...
	my ($rc,$dets)=$DB->get_details_by_sha1(sha1 => $document_sha1);
	if($rc!=1) {
		return "Error $rc from get_details_by_sha1!";
	}
	my ($rc2,$dets2)=$DB->get_user_sections(user => $v, type => "reader");
	if($rc2!=1) {
		return "Error $rc2 from get_user_sections!";
	}
	# Cross references the users sections with the document
	# sections to ensure the user is allowed to view the document.
	my @doc_sections=split(/,/,$dets->{sections});
	my @user_read_sections=(@$dets2);
	
	my $allow_read=0;
	for my $csec (@doc_sections) {
		if(grep {$_ eq $csec} @user_read_sections) {
			$allow_read=1; last;
		}
	}

	if(! $allow_read) {
		return "You are not in the correct sections to read this document.";
	}

	# Ok, user can view document, so next step is to show history - first the
	# current details and then each revision - newest first...

	my ($rc3,$versions,$dets3)=$DB->get_versions(lname=>$lname);

	my $s="<table class=gri_container cols=2>";
	$s.="<tr class=gri_row>\n";
	$s.="<td class=gri_cell width=80%><font class=stdtextlarge>Current name: </font>$dets->{original_name}</td>";
	$s.="<td class=gri_cell-right width=20%><font class=stdtextlarge>Versions: </font>$versions</td>";
	$s.="</tr>\n";
	$s.="<tr class=gri_row>\n";
	$s.="<td class=gri_cell colspan=2><font class=stdtextlarge>Description</font><br>"; 
	$s.=decode_base64($dets->{description}) . "</td>";
	$s.="</tr>\n<tr class=gri_row><td class=gri_cell colspan=2>&nbsp;</td></tr>";
	
	$s.="<tr class=gri_row><td class=gri_cell colspan=2>\n";
	$s.="<table class=gri_container cols=5>\n";
	$s.="<tr class=gri_headerrow>\n";
	$s.="<td class=gri_cell width=10%>Version</td>";
	$s.="<td class=gri_cell width=40%>Original Name</td>";
	$s.="<td class=gri_cell width=10%>Size</td>";
	$s.="<td class=gri_cell width=20%>Uploaded</td>";
	$s.="<td class=gri_cell width=20%>Actions</td>";
	$s.="</tr>\n";
	my $row=0;
	for my $cr (@$dets3) {
		my $_name=$cr->{original_name};
		$_name =~ s/.*\\//;
		$_name =~ s/.*\///;
		my $uploaded=$cr->{uploaded_time};
		my $size=0;
		print STDERR "$db_dir/$cr->{local_name}\n";
		my @SS=stat("$db_dir/$cr->{local_name}");
		if(@SS) {
			$size=$SS[7];
		}
		$size=sprintf("%9.1f Kb",$size/1024);
		$size =~ s/ /&nbsp;/g;
		
		my @lt=localtime($uploaded);
		$uploaded=sprintf("%02d/%02d/%04d %02d:%02d",$lt[3],$lt[4]+1,$lt[5]+1900,$lt[2],$lt[1]);
		my $actions="&nbsp;";
		my $l=link_to_str("stdlink","View","docrepos_view_historic_document","section=" . _encode_base64($section) . ",document_sha1=$cr->{content_sha1},lname=$cr->{local_name},lpname=$lname");
		$actions.="$l&nbsp;&nbsp;";

		$class="gri_odd";
		$class="gri_even" if $row%2==0;
		$s.="<tr class=gri_row id=$class>";
		$s.="<td class=gri_cell align=left>$cr->{version}</td>\n";
		$s.="<td class=gri_cell align=left>$_name</td>\n";
		$s.="<td class=gri_cell align=left>$size</td>\n";
		$s.="<td class=gri_cell align=left>$uploaded</td>\n";
		$s.="<td class=gri_cell align=left>$actions</td>\n";
		$s.="</tr>\n";
		$row++;
	}

	$s.="</table>\n";
	$s.="</td></tr></table>\n";

	$s.="</center>";
	return $s;
}

1;

